# -*- coding: utf-8 -*-
import ftplib
import hashlib
import os

host = 'localhost'
username = 'lxl'
pwd = '8901'
port = 21
filePath = '/home/lxl/python_ftp/ftp_commit/upload_file.txt'
serverPath = '/home/lxl/python_ftp/ftp_commit/'

session = ftplib.FTP()

session.connect(host, port)

session.login(username, pwd)

#链接成功，设置远程目录
session.cwd(serverPath)

file = open(filePath, 'rb')

filename, file_extension = os.path.splitext(filePath)

str_hash = hashlib.sha1(file.read()).hexdigest()

target_file_name = str_hash[:16]+file_extension

# 前面的file.read使得file的read cursor指向了文件的尾部
# 如果不手动调整指针的话，后面的文件上传，就会是一个空文件
file.seek(0)

session.storbinary('STOR '+target_file_name, file)

file.close()

session.quit()

